﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;

using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
using NPOI.SS.Util;


namespace IHISCETeim.Web.Export.FTradeModel
{
    public partial class FTradeExportWeekReport : ExportPageBase
    {
        protected override string xlsTemplateFileName
        {
            get
            {
                return "应收帐款_财务部周报格式.xls";
            }
        }
        protected override string SheetName
        {
            get
            {
                return "本周销售、收款";
            }
        }


        protected string CreateDateFrom
        {
            get
            {
                DateTime dt = DateTime.Now; //当前时间
                DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d"))); //本周周一
                DateTime endWeek = startWeek.AddDays(6); //本周周日
                //return startWeek.ToString("yyyy-MM-dd");

                return GetRequestValue<string>("CreateDateFrom", startWeek.ToString("yyyy-MM-dd"));
            }
        }
        protected string CreateDateTo
        {
            get
            {
                DateTime dt = DateTime.Now; //当前时间
                DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d"))); //本周周一
                DateTime endWeek = startWeek.AddDays(6); //本周周日

                return GetRequestValue<string>("CreateDateTo", endWeek.ToString("yyyy-MM-dd"));
                
            }
        }

        protected DataTable sumdt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            string wheresql = "";
            wheresql = string.Format(" CreateDate between '{0}' and '{1} 23:59:59' ",CreateDateFrom,CreateDateTo);
            string SQLSort = " JiHao desc,FTradeID ASC";
            int TotalCount = 0;
            DataTableCollection dts = FTradeReceivablesController.FTradeReceivablesHistoryGetPagination(wheresql, 1, 99999, SQLSort, ref TotalCount);

            DataTable exportDT = dts[0];

            InitializeWorkbook();

            ISheet sheet1 = hssfworkbook.GetSheet(SheetName);
            //写标题
            string DTTitle = string.Format("{0}到{1}", CreateDateFrom, CreateDateTo);// 2013年05月13日至2013年05月17日
            sheet1.GetRow(0).GetCell(0).SetCellValue(DTTitle);

            //FOR写入
            int rowindexBase = 6;
            int rowindex = 0;
            int rowReal = rowindex + rowindexBase;
            foreach (DataRow dr in exportDT.Rows)
            {


                HSSFRow row1 = (HSSFRow)sheet1.CreateRow(rowReal);
                // row1.CreateCell(0).SetCellValue(1);
                row1.CreateCell(0).SetCellValue((rowReal-5).ToString()+".");
                row1.CreateCell(1).SetCellValue(dr["JingXiaoShang"].ToString());
                row1.CreateCell(2).SetCellValue(dr["HeTongHao"].ToString());
                row1.CreateCell(3).SetCellValue(dr["XingHao"].ToString());
                row1.CreateCell(4).SetCellValue(dr["JiHao"].ToString());
                row1.CreateCell(5).SetCellValue(GetDBDouble(dr["ShuLiang"]));
                row1.CreateCell(6).SetCellValue(GetDBDouble(dr["Price"])/10000);
                row1.CreateCell(7).SetCellValue(GetDBDouble(dr["GPSFeiYong"]) / 10000);
                row1.CreateCell(8).SetCellValue(GetDBDouble(dr["YueCuXiaoJinE"]) / 10000);
                row1.CreateCell(9).SetCellValue(GetDBDouble(dr["HeTongJinE"]) / 10000);
                row1.CreateCell(10).SetCellValue(dr["FuKuanTiaoKuan"].ToString());
                row1.CreateCell(11).SetCellValue(dr["GPSNum"].ToString());

                rowindex++;
                rowReal = rowindex + rowindexBase;
            }

            

            sheet1.ForceFormulaRecalculation = true;

            WriteToFile();
        }

        private Double GetDBDouble(object v)
        {
            if (v is DBNull)
                return 0;
            return Convert.ToDouble(v);
        }
        protected override string GetXlsFileName(string oldFileName)
        {
            //return base.GetXlsFileName(oldFileName);

            return string.Format("{0}至{1}内销情况表.xls",CreateDateFrom,CreateDateTo );
        }

    }
}